#ifndef  ZIVIDCAMERA_H__
#define  ZIVIDCAMERA_H__

#include "zividcamera.h"

int zivid_camerainit(pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPtr)
{
    try
    {
        Zivid::Application zivid;
        auto camera = zivid.connectCamera();

        const auto settings = Zivid::Settings{ Zivid::Settings::Acquisitions{
            Zivid::Settings::Acquisition{ Zivid::Settings::Acquisition::Aperture{ 5.66 } } } };

        const auto frame = camera.capture(settings);
        const auto pointCloud = frame.pointCloud();
        const auto data = pointCloud.copyData<Zivid::PointXYZColorRGBA>();

        pcl::PointCloud<pcl::PointXYZ> pointCloudPCL;

        pointCloudPCL.width = pointCloud.width();
        pointCloudPCL.height = pointCloud.height();
        pointCloudPCL.is_dense = false;
        pointCloudPCL.points.resize(pointCloudPCL.width * pointCloudPCL.height);

        for(size_t i = 0; i < pointCloudPCL.points.size(); ++i)
        {
            pointCloudPCL.points[i].x = data(i).point.x; // NOLINT(cppcoreguidelines-pro-type-union-access)
            pointCloudPCL.points[i].y = data(i).point.y; // NOLINT(cppcoreguidelines-pro-type-union-access)
            pointCloudPCL.points[i].z = data(i).point.z; // NOLINT(cppcoreguidelines-pro-type-union-access)
            // pointCloudPCL.points[i].r = data(i).color.r; // NOLINT(cppcoreguidelines-pro-type-union-access)
            // pointCloudPCL.points[i].g = data(i).color.g; // NOLINT(cppcoreguidelines-pro-type-union-access)
            // pointCloudPCL.points[i].b = data(i).color.b; // NOLINT(cppcoreguidelines-pro-type-union-access)
        }

        //pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPtr(new pcl::PointCloud<pcl::PointXYZRGB>);
        *cloudPtr = pointCloudPCL;

        std::string pointCloudFile = "Zivid3D.pcd";
        std::cout << "Saving point cloud to file: " << pointCloudFile << std::endl;
        pcl::io::savePCDFileBinary(pointCloudFile, pointCloudPCL);
        // pcl::visualization::CloudViewer cloudViewer("Simple Cloud Viewer");
        // cloudViewer.showCloud(cloudPtr);
        // while(!cloudViewer.wasStopped())
        // {
            
        // }
    }
    catch(const std::exception &e)
    {
        std::cerr << "Error: " << Zivid::toString(e) << std::endl;
        std::cout << "Press enter to exit." << std::endl;
        std::cin.get();
        return EXIT_FAILURE;
    }
    return 0;
}


#endif